home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / rzsz9107.zip / RBSB.C < prev    next >
C/C++ Source or Header  |  1990-12-09  |  7KB  |  332 lines

  1. /*
  2.  *
  3.  *  Rev 5-09-89
  4.  *  This file contains Unix specific code for setting terminal modes,
  5.  *  very little is specific to ZMODEM or YMODEM per se (that code is in
  6.  *  sz.c and rz.c).  The CRC-16 routines used by XMODEM, YMODEM, and ZMODEM
  7.  *  are also in this file, a fast table driven macro version
  8.  *
  9.  *    V7/BSD HACKERS:  SEE NOTES UNDER mode(2) !!!
  10.  *
  11.  *   This file is #included so the main file can set parameters such as HOWMANY.
  12.  *   See the main files (rz.c/sz.c) for compile instructions.
  13.  */
  14.  
  15. #ifdef V7
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #define STAT
  19. #include <sgtty.h>
  20. #define OS "V7/BSD"
  21. #define ROPMODE "r"
  22. #ifdef LLITOUT
  23. long Locmode;        /* Saved "local mode" for 4.x BSD "new driver" */
  24. long Locbit = LLITOUT;    /* Bit SUPPOSED to disable output translations */
  25. #include <strings.h>
  26. #endif
  27. #endif
  28.  
  29. #ifndef OS
  30. #ifndef USG
  31. #define USG
  32. #endif
  33. #endif
  34.  
  35. #ifdef USG
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #define STAT
  39. #include <termio.h>
  40. #define OS "SYS III/V"
  41. #define ROPMODE "r"
  42. #define MODE2OK
  43. #include <string.h>
  44. #endif
  45.  
  46. #ifdef T6K
  47. #include <sys/ioctl.h>        /* JPRadley: for the Tandy 6000 */
  48. #endif
  49.  
  50. #if HOWMANY  > 255
  51. Howmany must be 255 or less
  52. #endif
  53.  
  54. /*
  55.  * return 1 iff stdout and stderr are different devices
  56.  *  indicating this program operating with a modem on a
  57.  *  different line
  58.  */
  59. int Fromcu;        /* Were called from cu or yam */
  60. from_cu()
  61. {
  62.     struct stat a, b;
  63.  
  64.     fstat(1, &a); fstat(2, &b);
  65.     Fromcu = a.st_rdev != b.st_rdev;
  66.     return;
  67. }
  68. cucheck()
  69. {
  70.     if (Fromcu)
  71.         fprintf(stderr,"Please read the manual page BUGS chapter!\r\n");
  72. }
  73.  
  74.  
  75. struct {
  76.     unsigned baudr;
  77.     int speedcode;
  78. } speeds[] = {
  79.     110,    B110,
  80.     300,    B300,
  81.     600,    B600,
  82.     1200,    B1200,
  83.     2400,    B2400,
  84.     4800,    B4800,
  85.     9600,    B9600,
  86.     19200,    EXTA,
  87.     38400,    EXTB,
  88.     0,
  89. };
  90.  
  91. int Twostop;        /* Use two stop bits */
  92.  
  93.  
  94. /*
  95.  *  The following uses an external rdchk() routine if available,
  96.  *  otherwise defines the function for BSD or fakes it for SYSV.
  97.  */
  98.  
  99. #ifndef READCHECK
  100. #ifdef FIONREAD
  101. #define READCHECK
  102. /*
  103.  *  Return non 0 iff something to read from io descriptor f
  104.  */
  105. rdchk(f)
  106. {
  107.     static long lf;
  108.  
  109.     ioctl(f, FIONREAD, &lf);
  110.     return ((int) lf);
  111. }
  112.  
  113. #else        /* FIONREAD */
  114.  
  115. #ifdef SV
  116. #define READCHECK
  117. #include <fcntl.h>
  118.  
  119. int checked = 0;
  120. /*
  121.  * Nonblocking I/O is a bit different in System V, Release 2
  122.  *  Note: this rdchk vsn throws away a byte, OK for ZMODEM
  123.  *  sender because protocol design anticipates this problem.
  124.  */
  125. #define EATSIT
  126. rdchk(f)
  127. {
  128.     int lf, savestat;
  129.     static char bchecked;
  130.  
  131.     savestat = fcntl(f, F_GETFL) ;
  132.     fcntl(f, F_SETFL, savestat | O_NDELAY) ;
  133.     lf = read(f, &bchecked, 1) ;
  134.     fcntl(f, F_SETFL, savestat) ;
  135.     checked = bchecked & 0377;    /* force unsigned byte */
  136.     return(lf) ;
  137. }
  138. #endif
  139. #endif
  140. #endif
  141.  
  142.  
  143. static unsigned
  144. getspeed(code)
  145. {
  146.     register n;
  147.  
  148.     for (n=0; speeds[n].baudr; ++n)
  149.         if (speeds[n].speedcode == code)
  150.             return speeds[n].baudr;
  151.     return 38400;    /* Assume fifo if ioctl failed */
  152. }
  153.  
  154.  
  155.  
  156. #ifdef ICANON
  157. struct termio oldtty, tty;
  158. #else
  159. struct sgttyb oldtty, tty;
  160. struct tchars oldtch, tch;
  161. #endif
  162.  
  163. /*
  164.  * mode(n)
  165.  *  3: save old tty stat, set raw mode with flow control
  166.  *  2: set XON/XOFF for sb/sz with ZMODEM or YMODEM-g
  167.  *  1: save old tty stat, set raw mode 
  168.  *  0: restore original tty mode
  169.  */
  170. mode(n)
  171. {
  172.     static did0 = FALSE;
  173.  
  174.     vfile("mode:%d", n);
  175.     switch(n) {
  176. #ifdef USG
  177.     case 2:        /* Un-raw mode used by sz, sb when -g detected */
  178.         if(!did0)
  179.             (void) ioctl(0, TCGETA, &oldtty);
  180.         tty = oldtty;
  181.  
  182.         tty.c_iflag = BRKINT|IXON;
  183.  
  184.         tty.c_oflag = 0;    /* Transparent output */
  185.  
  186.         tty.c_cflag &= ~PARENB;    /* Disable parity */
  187.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  188.         if (Twostop)
  189.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  190.  
  191.  
  192. #ifdef READCHECK
  193.         tty.c_lflag = Zmodem ? 0 : ISIG;
  194.         tty.c_cc[VINTR] = Zmodem ? -1:030;    /* Interrupt char */
  195. #else
  196.         tty.c_lflag = ISIG;
  197.         tty.c_cc[VINTR] = Zmodem ? 03:030;    /* Interrupt char */
  198. #endif
  199.         tty.c_cc[VQUIT] = -1;            /* Quit char */
  200. #ifdef NFGVMIN
  201.         tty.c_cc[VMIN] = 1;
  202. #else
  203.         tty.c_cc[VMIN] = 3;     /* This many chars satisfies reads */
  204. #endif
  205.         tty.c_cc[VTIME] = 1;    /* or in this many tenths of seconds */
  206.  
  207.         (void) ioctl(0, TCSETAW, &tty);
  208.         did0 = TRUE;
  209.         return OK;
  210.     case 1:
  211.     case 3:
  212.         if(!did0)
  213.             (void) ioctl(0, TCGETA, &oldtty);
  214.         tty = oldtty;
  215.  
  216.         tty.c_iflag = n==3 ? (IGNBRK|IXOFF) : IGNBRK;
  217.  
  218.          /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
  219.         tty.c_lflag &= ~(ECHO | ICANON | ISIG);
  220.  
  221.         tty.c_oflag = 0;    /* Transparent output */
  222.  
  223.         tty.c_cflag &= ~PARENB;    /* Same baud rate, disable parity */
  224.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  225.         if (Twostop)
  226.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  227. #ifdef NFGVMIN
  228.         tty.c_cc[VMIN] = 1; /* This many chars satisfies reads */
  229. #else
  230.         tty.c_cc[VMIN] = HOWMANY; /* This many chars satisfies reads */
  231. #endif
  232.         tty.c_cc[VTIME] = 1;    /* or in this many tenths of seconds */
  233.         (void) ioctl(0, TCSETAW, &tty);
  234.         did0 = TRUE;
  235.         Effbaud = Baudrate = getspeed(tty.c_cflag & CBAUD);
  236.         return OK;
  237. #endif
  238. #ifdef V7
  239.     /*
  240.      *  NOTE: this should transmit all 8 bits and at the same time
  241.      *   respond to XOFF/XON flow control.  If no FIONREAD or other
  242.      *   rdchk() alternative, also must respond to INTRRUPT char
  243.      *   This doesn't work with V7.  It should work with LLITOUT,
  244.      *   but LLITOUT was broken on the machine I tried it on.
  245.      */
  246.     case 2:        /* Un-raw mode used by sz, sb when -g detected */
  247.         if(!did0) {
  248.             ioctl(0, TIOCEXCL, 0);
  249.             ioctl(0, TIOCGETP, &oldtty);
  250.             ioctl(0, TIOCGETC, &oldtch);
  251. #ifdef LLITOUT
  252.             ioctl(0, TIOCLGET, &Locmode);
  253. #endif
  254.         }
  255.         tty = oldtty;
  256.         tch = oldtch;
  257. #ifdef READCHECK
  258.         tch.t_intrc = Zmodem ? -1:030;    /* Interrupt char */
  259. #else
  260.         tch.t_intrc = Zmodem ? 03:030;    /* Interrupt char */
  261. #endif
  262.         tty.sg_flags |= (ODDP|EVENP|CBREAK);
  263.         tty.sg_flags &= ~(ALLDELAY|CRMOD|ECHO|LCASE);
  264.         ioctl(0, TIOCSETP, &tty);
  265.         ioctl(0, TIOCSETC, &tch);
  266. #ifdef LLITOUT
  267.         ioctl(0, TIOCLBIS, &Locbit);
  268. #endif
  269.         bibi(99);    /* un-raw doesn't work w/o lit out */
  270.         did0 = TRUE;
  271.         return OK;
  272.     case 1:
  273.     case 3:
  274.         if(!did0) {
  275.             ioctl(0, TIOCEXCL, 0);
  276.             ioctl(0, TIOCGETP, &oldtty);
  277.             ioctl(0, TIOCGETC, &oldtch);
  278. #ifdef LLITOUT
  279.             ioctl(0, TIOCLGET, &Locmode);
  280. #endif
  281.         }
  282.         tty = oldtty;
  283.         tty.sg_flags |= (RAW|TANDEM);
  284.         tty.sg_flags &= ~ECHO;
  285.         ioctl(0, TIOCSETP, &tty);
  286.         did0 = TRUE;
  287.         Effbaud = Baudrate = getspeed(tty.sg_ospeed);
  288.         return OK;
  289. #endif
  290.     case 0:
  291.         if(!did0)
  292.             return ERROR;
  293. #ifdef USG
  294.         (void) ioctl(0, TCSBRK, 1);    /* Wait for output to drain */
  295.         (void) ioctl(0, TCFLSH, 1);    /* Flush input queue */
  296.         (void) ioctl(0, TCSETAW, &oldtty);    /* Restore modes */
  297.         (void) ioctl(0, TCXONC,1);    /* Restart output */
  298. #endif
  299. #ifdef V7
  300.         ioctl(0, TIOCSETP, &oldtty);
  301.         ioctl(0, TIOCSETC, &oldtch);
  302.         ioctl(0, TIOCNXCL, 0);
  303. #ifdef LLITOUT
  304.         ioctl(0, TIOCLSET, &Locmode);
  305. #endif
  306. #endif
  307.  
  308.         return OK;
  309.     default:
  310.         return ERROR;
  311.     }
  312. }
  313.  
  314. sendbrk()
  315. {
  316. #ifdef V7
  317. #ifdef TIOCSBRK
  318. #define CANBREAK
  319.     sleep(1);
  320.     ioctl(0, TIOCSBRK, 0);
  321.     sleep(1);
  322.     ioctl(0, TIOCCBRK, 0);
  323. #endif
  324. #endif
  325. #ifdef USG
  326. #define CANBREAK
  327.     ioctl(0, TCSBRK, 0);
  328. #endif
  329. }
  330.  
  331. /* End of rbsb.c */
  332.